package indi.gao.redisTemplate;

import java.io.*;
import java.util.*;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020$-02$-20$</P>
 * <P>@version 1.0</P>
 */
public class tope {
    public static void main(String[] args) throws IOException {
//        List<String> list = dataList("F:\\EnglishDev\\UnitTest\\TxtReadAndWrite\\src\\main\\file\\oldSql.txt");
//        for (String s : list) {
//            Map<String, Object> stringObjectMap = oralSqlStringTrans(s);
//            System.out.println(stringObjectMap);
//        }
//        ArrayList<String> objects = new ArrayList<>();
//        objects.add("sdf");
//        System.out.println(objects.toString());
        String fileName = "F:\\\\EnglishDev\\\\UnitTest\\\\TxtReadAndWrite\\\\src\\\\main\\\\file\\\\oldSql.txt";
        List<String> list = dealEach(fileName);
        String resultFileName = "F:\\EnglishDev\\UnitTest\\TxtReadAndWrite\\src\\main\\file\\oldSqlResult.txt";
        writeIntoTxt(list, resultFileName);
    }

    // 读取text数据
    private static List<String> dataList(String filePath) throws IOException {
        List<String> dataStringList = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
        String lineText;
        while ((lineText = bufferedReader.readLine()) != null) {
            dataStringList.add(lineText);
        }
        bufferedReader.close();
        return dataStringList;
    }

    // 将数据写入到txt
    private static void writeIntoTxt(List<String> stringList, String file) throws IOException {
        // bufferedWriter 未关闭之前可以叠加写入，关闭之后覆盖写入
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(file))));
        for (String s : stringList) {
            bufferedWriter.write(s);
            bufferedWriter.newLine();
        }
        bufferedWriter.close();
    }

    //格式化每条sql
    private static Map<String, Object> oralSqlStringTrans(String oldSql) {

        //找到values的位置
        int values = oldSql.indexOf("VALUES");
        //得到第一个左括号的索引
        int left1 = oldSql.indexOf('(');
        // 得到第二个左括号索引的位置
        int left2 = oldSql.indexOf('(', values);
        // 得到第一个有括弧的位置
        int right1 = oldSql.indexOf(')', values - 10);
        //得到第二个有括弧的位置
        int right2 = oldSql.lastIndexOf(')');

        // insert 字段
        String substring = oldSql.substring(0, left1);
        //data字段
        String substring1 = oldSql.substring(left1 + 1, right1);
        String[] split = substring1.split(",");
        List<String> list = Arrays.asList(split);
        //value 字段
        String substring2 = oldSql.substring(left2 + 1, right2);
        String[] split1 = substring2.split(",");
        List<String> list1 = Arrays.asList(split1);

        Map<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("pre", substring);
        objectObjectHashMap.put("data", list);
        objectObjectHashMap.put("value", list1);
        return objectObjectHashMap;
    }

    // 进行处理
    private static List<String> dealEach(String file) throws IOException {
        List<String> objects = new ArrayList<>();
        //读取数据
        List<String> list = dataList(file);
        //格式化数据
        for (String s : list) {
            Map<String, Object> stringObjectMap = oralSqlStringTrans(s);
            String s1 = dealOral(stringObjectMap);
            objects.add(s1);
        }
        return objects;
    }

    // 处理口语表
    private static String dealOral(Map<String, Object> stringObjectMap) {
        List<String> dataList0 = (List<String>) stringObjectMap.get("data");
        List<String> dataList = new ArrayList<>(dataList0);
        List<String> valueList0 = (List<String>) stringObjectMap.get("value");
        List<String> valueList = new ArrayList<>(valueList0);
        String pre = (String) stringObjectMap.get("pre");
        pre = pre.replaceAll("`common-base6`.", "");
        // 第一个字段改成 oralId
        dataList.set(0, "`readOralLogId`");
        dataList.set(1, "`studentId`");
        dataList.set(2, "`oralId`");
        for (int i = 0; i < 4; i++) {
            dataList.remove(7);
            valueList.remove(7);
        }
        for (int i = 0; i < 2; i++) {
            dataList.remove(10);
            valueList.remove(10);
        }
        String s = resultSql(dataList, valueList, pre);
        return s;
    }

//    INSERT INTO `ol_read_oral_log`(`readOralLogID`, `studentID`, `oralID`, `oralLevel`, `readWordNum`, `readSentenceNum`,
// `readSecond`, `readAvgScore`, `anwserScore`, `isComplete`, `readOverTime`, `isValid`, `createTime`,
// `updateTime`, `remarks`, `isDone`, `skipWordNum`, `skipSentenceNum`)
// VALUES (1365, 9630, 1917, 1, 5, 5, 89, NULL, NULL, 1, '2018-04-09 10:35:05', 0,
// '2018-04-09 10:35:05', '2018-04-09 10:35:05', '2019-11-18T10:04:03.533+08:00reset', 0, 0, 0);

    private static String resultSql(List<String> dataList, List<String> valueList, String pre) {
        pre = pre + "(";
        String string1 = dataList.toString();
        String s = string1.replaceAll("\\[", "").replaceAll("]", "");
        s = s + ") VALUES (";
        String string = valueList.toString();
        String st = string.replaceAll("\\[", "").replaceAll("]", "");
        s = pre + s + st + ");";
        return s;
    }

    private static class Extract {
        private Map<String, Object> stringObjectMap;
        private List<String> dataList;
        private List<String> valueList;
        private String pre;

        public Extract(Map<String, Object> stringObjectMap) {
            this.stringObjectMap = stringObjectMap;
        }

        public List<String> getDataList() {
            return dataList;
        }

        public List<String> getValueList() {
            return valueList;
        }

        public String getPre() {
            return pre;
        }

        public Extract invoke() {
            List<String> dataList0 = (List<String>) stringObjectMap.get("data");
            dataList = new ArrayList<>(dataList0);
            List<String> valueList0 = (List<String>) stringObjectMap.get("value");
            valueList = new ArrayList<>(valueList0);
            pre = (String) stringObjectMap.get("pre");
            return this;
        }
    }
}
